package com;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class FileCopy extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton[] bt = new JButton[2];
	private JTextField[] jtf = new JTextField[2];
	private JProgressBar jpb = new JProgressBar();
	private JButton jbttwo;
	private File srcFile;

	public FileCopy() {
		setTitle("复制文件");
		setVisible(true);
		setSize(400, 500);
		setLocation(500, 100);
		setLayout(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		String[] str = { "源文件：", "目标路径：" };
		for (int i = 0; i < str.length; i++) {
			JLabel label = new JLabel(str[i]);
			label.setSize(100, 25);
			label.setLocation(30, 50 + i * 40);
			add(label);
			jtf[i] = new JTextField(10);
			jtf[i].setSize(180, 25);
			jtf[i].setLocation(100, 50 + i * 40);
			jtf[i].setEnabled(false);
			add(jtf[i]);
			bt[i] = new JButton("...");
			bt[i].setSize(50, 25);
			bt[i].setLocation(300, 50 + i * 40);
			add(bt[i]);
			bt[i].addActionListener(this);
		}
		jpb = new JProgressBar(0, 100);
		jpb.setSize(300, 25);
		jpb.setLocation(40, 430);
		jpb.setStringPainted(true);
		jpb.setValue(0);
		add(jpb);
		jbttwo = new JButton("Copy");
		jbttwo.setSize(300, 25);
		jbttwo.setLocation(40, 200);
		jbttwo.addActionListener(this);
		add(jbttwo);
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				new FileCopy();

			}
		});

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		if (obj == bt[0]) {
			JFileChooser jfc = new JFileChooser();
			int f = jfc.showOpenDialog(this);
			if (f == JFileChooser.APPROVE_OPTION) {
				srcFile = jfc.getSelectedFile();
				jtf[0].setText(srcFile.getPath());
			}
		} else if (obj == bt[1]) {
			JFileChooser jfctwo = new JFileChooser();
			jfctwo.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int ftwo = jfctwo.showOpenDialog(this);
			if (ftwo == JFileChooser.APPROVE_OPTION) {
				jtf[1].setText(jfctwo.getSelectedFile().getPath());
			}
		} else if (obj == jbttwo) {
			File desFile = new File(jtf[1].getText() + "\\" + srcFile.getName());
			copyFile(srcFile, desFile);
		}
	}

	private void copyFile(File srcFile, File desFile) {
		new Thread(new PbThread(jpb, srcFile, desFile)).start();
		;
	}

}
